using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Business.ProcurementManagement.Dto;
using Business.Models;
using Microsoft.AspNetCore.Authorization;
using Business.Permissions;
using Microsoft.CodeAnalysis;
using System.Threading;

namespace Business.ProcurementManagement
{
    [Authorize(BusinessPermissions.Procurement.Default)]
    public class ProcurementAppService : ApplicationService, IProcurementAppService
    {
        private const string FormName = "procurement";
        private IRepository<Procurement, Guid> _repository;
        private IRepository<Formmian, Guid> _repository1;
        public ProcurementAppService(
            IRepository<Procurement, Guid> repository,
                IRepository<Formmian, Guid> repository1
            )
        {
            _repository = repository;
            _repository1 = repository1;
        }
        #region 增删改查基础方法

        public async Task<ProcurementDto> Get(Guid id)
        {
            var data = await _repository.GetAsync(id);
            var dto = ObjectMapper.Map<Procurement, ProcurementDto>(data);
            return dto;
        }

        public async Task<PagedResultDto<ProcurementDto>> GetAll(GetProcurementInputDto input)
        {
            var listmodel = await _repository.GetListAsync();
            var reslist = await _repository1.GetListAsync();
            var query = (from a in listmodel
                         join b in reslist on a.Trackingnumber equals b.Trackingnumber
                         select new ProcurementDto
                         {
                             Trackingnumber = a.Trackingnumber,
                             Inboundordernumber = a.Inboundordernumber,
                             Externaltrackingnumber = a.Externaltrackingnumber,
                             Carriers=a.Carriers
                         }).AsQueryable();


            if (!string.IsNullOrEmpty(input.Trackingnumber))
            {
                query = query.Where(x => x.Trackingnumber.Contains(input.Trackingnumber));
            }
            if (!string.IsNullOrEmpty(input.Inboundordernumber))
            {
                query = query.Where(x => x.Inboundordernumber.Contains(input.Inboundordernumber));
            }
            if (!string.IsNullOrEmpty(input.Externaltrackingnumber))
            {
                query = query.Where(x => x.Externaltrackingnumber.Contains(input.Externaltrackingnumber));
            }
            if (input.Carriers !=0)
            {
                query = query.Where(x => x.Carriers == input.Carriers);
            }


            var totalCount = query.Count();
            var items = query.OrderBy(input.Sorting ?? "Id")
                       .Skip(input.SkipCount == 0 ? input.SkipCount : 0)
                       .Take(input.MaxResultCount)
                       .ToList();

            //var dto = ObjectMapper.Map<List<Inventoryquantity>, List<InventoryquantityDto>>(items);
            var dto = new PagedResultDto<ProcurementDto>(totalCount, items);
            return dto;
        }

        public async Task<ProcurementDto> DataPost(CreateOrUpdateProcurementDto input)
        {
            Procurement result = null;
            if (!input.Id.HasValue)
            {
                input.Id = GuidGenerator.Create();
                result = await _repository.InsertAsync(ObjectMapper.Map<CreateOrUpdateProcurementDto, Procurement>(input));
            }
            else
            {
                var data = await _repository.GetAsync(input.Id.Value);
                result = await _repository.UpdateAsync(ObjectMapper.Map(input, data));
            }
            return ObjectMapper.Map<Procurement, ProcurementDto>(result);
        }

        public async Task Delete(List<Guid> ids)
        {
            foreach (var item in ids)
            {
                await _repository.DeleteAsync(item);
            }

        }


        #endregion

    }
}